package com.run_java_test.test5;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试
 */
@RestController
public class HelloController {

    private final TestDynamicLoad testDynamicLoad;

    @Autowired
    public HelloController(TestDynamicLoad testDynamicLoad) {
        this.testDynamicLoad = testDynamicLoad;
    }

    @RequestMapping("/")
    String hello() {
        return "Hello";
    }

    /**
     * @param path     jar文件的路径
     * @param fileName jar文件的名称
     * @return 加载结果
     */
    @RequestMapping("/load")
    String load(@RequestParam String path, @RequestParam String fileName) {
        try {
            testDynamicLoad.loadJar(path, fileName);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
            return "失败:" + e.getMessage();
        }
        return "加载成功";
    }


    /**
     * @param name 卸载jar的名称
     * @return
     */
    @RequestMapping("/unload")
    String unload(@RequestParam String name) {
        try {
            testDynamicLoad.unloadJar(name);
        } catch (IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
            return "失败:" + e.getMessage();
        }
        return "卸载成功";
    }
}